package com.zhongfl.crm.service.impl.boss;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhongfl.crm.enums.boss.AmibaStaffRole;
import com.zhongfl.crm.exception.CRMException;
import com.zhongfl.crm.lixiao.mapper.AmibaStaffMapper;
import com.zhongfl.crm.lixiao.mapper.WarrantMapper;
import com.zhongfl.crm.lixiao.mapper.WorkOrderMapper;
import com.zhongfl.crm.model.boss.*;
import com.zhongfl.crm.service.boss.AttachmentService;
import com.zhongfl.crm.service.boss.WarrantService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tech.heptagram.channel.api.common.AssertUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 权证管理service实现类
 *
 * @author zhangzengke
 * @date 2018/10/23
 */
@Service
public class WarrantServiceImpl implements WarrantService {

    private static final String HOUSE_PIC = "WARRANT_HOUSE_PIC";

    private static final String EVIDENCE_PIC = "WARRANT_EVIDENCE_PIC";

    @Resource
    private WarrantMapper warrantMapper;

    @Resource
    private WorkOrderMapper workOrderMapper;

    @Resource
    private AttachmentService attachmentService;

    @Resource
    private AmibaStaffMapper amibaStaffMapper;

    @Override
    public PageInfo<WarrantWorkOrderDetail> findWarrantList(WorkOrderQuery workOrderQuery) {
        PageHelper.startPage(workOrderQuery.getPage(), workOrderQuery.getPageSize());

        List<WarrantWorkOrderDetail> warrantWorkOrders = warrantMapper.queryWarrantWorkOrder(workOrderQuery);
        return new PageInfo<>(warrantWorkOrders);
    }

    @Override
    public void saveUploadInfo(RxWarrantWorkOrderFileUpload uploadInfo) {

        AssertUtils.notNull(uploadInfo, "要上传的凭证为Null");
        if (CollectionUtils.isEmpty(uploadInfo.getFilePaths())) {
            throw new CRMException("要先上传图片");
        }
        WorkOrder workOrder = workOrderMapper.findByWorkOrderNo(uploadInfo.getWorkOrderNo());
        if(Objects.isNull(workOrder.getClaimStaffCode())){
            throw new CRMException("请先分配此工单！");
        }

        AmibaStaff operStaff = amibaStaffMapper.selectByStaffCodeAndAmibaId(uploadInfo.getOperCode(),uploadInfo.getReceiveAmibaId());
        // 不是巴长, 也不是工单执行人, 不能操作图片
        if (operStaff.getRole() != AmibaStaffRole.MANAGER.getValue()
                && !uploadInfo.getOperCode().equals(workOrder.getClaimStaffCode())) {
            throw new CRMException("您不是该工单的处理人或权证巴巴长，没有权限上传图片！");
        }else {
            for (String filePath : uploadInfo.getFilePaths()) {
                WarrantWorkOrderAttachment warrantWorkOrderAttachment = new WarrantWorkOrderAttachment();
                warrantWorkOrderAttachment.setOrderNo(uploadInfo.getOrderNo());
                warrantWorkOrderAttachment.setWorkOrderNo(uploadInfo.getWorkOrderNo());
                warrantWorkOrderAttachment.setKey(uploadInfo.getKey());
                warrantWorkOrderAttachment.setPath(filePath);
                warrantWorkOrderAttachment.setCreatedBy(uploadInfo.getOperName());
                warrantWorkOrderAttachment.setModifiedBy(uploadInfo.getOperName());
                warrantMapper.insertWarrantAttachment(warrantWorkOrderAttachment);
            }
        }
    }

    @Override
    public List<WarrantWorkOrderAttachment> selectByWorkOrderNo(String workOrderNo) {
        AssertUtils.notNull(workOrderNo, "工单号不能为空");

        return warrantMapper.selectByWorkOrderNo(workOrderNo);
    }

    @Override
    public List<OrderAttachment> getHousePic(String workOrderNo) {

        List<OrderAttachment> list = new ArrayList<>();
        //根据工单号查找权证工单图片信息
        List<WarrantWorkOrderAttachment> warrantWorkOrderAttachments = selectByWorkOrderNo(workOrderNo);

        for (WarrantWorkOrderAttachment warrantWorkOrderAttachment : warrantWorkOrderAttachments) {
            if (warrantWorkOrderAttachment.getKey().equals(HOUSE_PIC)) {
                OrderAttachment houseOrderAttachment = new OrderAttachment();
                houseOrderAttachment.setPath(warrantWorkOrderAttachment.getPath());
                OrderAttachment orderAttachment = attachmentService.getUrl(houseOrderAttachment);
                orderAttachment.setId(warrantWorkOrderAttachment.getId());
                list.add(orderAttachment);
            }
        }
        return list;
    }

    @Override
    public List<OrderAttachment> getEvPic(String workOrderNo) {
        List<OrderAttachment> list = new ArrayList<>();
        //根据工单号查找权证工单图片信息
        List<WarrantWorkOrderAttachment> warrantWorkOrderAttachments = selectByWorkOrderNo(workOrderNo);

        for (WarrantWorkOrderAttachment warrantWorkOrderAttachment : warrantWorkOrderAttachments) {
            if (warrantWorkOrderAttachment.getKey().equals(EVIDENCE_PIC)) {
                OrderAttachment evOrderAttachment = new OrderAttachment();
                evOrderAttachment.setPath(warrantWorkOrderAttachment.getPath());
                OrderAttachment orderAttachment = attachmentService.getUrl(evOrderAttachment);
                orderAttachment.setId(warrantWorkOrderAttachment.getId());
                list.add(orderAttachment);
            }
        }
        return list;
    }

    @Override
    public void delete(Integer id) {

        warrantMapper.deleteAttachment(id);
    }
}
